package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.OrderBean;
import com.project.bean.ProductBean;
import com.project.bean.ShopBean;
import com.project.mapper.IOrderMapper;
import com.project.mapper.IShopMapper;
import com.project.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.w3c.dom.ls.LSException;

import java.time.LocalDate;
import java.util.List;
@Service
@Transactional
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private IOrderMapper mapper;
    @Autowired
    private IShopMapper shopMapper;
    @Override
    public void add(OrderBean orderBean, Integer userId) {
        orderBean.setUserId(userId);
        //添加订单
        mapper.insert(orderBean);
        //得到该用户购物车信息
        QueryWrapper<ShopBean>qw=new QueryWrapper<>();
        qw.eq("fk_userId",userId);
       List<ShopBean>shopList= shopMapper.selectList(qw);

       //添加订单明细
        mapper.addOrderInfo(orderBean.getId(),shopList);
        //情况该用户的购物车
        shopMapper.delete(qw);

    }

    @Override
    public IPage<OrderBean> findByUser(Integer userId, int pageNO) {
        QueryWrapper<OrderBean>qw=new QueryWrapper<>();
        qw.eq("fk_userId",userId);
        return mapper.selectPage(new Page<>(pageNO,PAGESIZE),qw);
    }

    @Override
    public OrderBean findById(int orderId) {
        return mapper.findById(orderId);
    }

    @Override
    public IPage<OrderBean> findByItem(int pageNO,String status, LocalDate startDate, LocalDate endDate) {
        return mapper.findByItem(new Page<OrderBean>(pageNO,PAGESIZE),status,startDate,endDate);
    }

    @Override
    public void updateStatus(int orderId, String status) {
        OrderBean orderBean= new OrderBean();
        orderBean.setId(orderId);
        orderBean.setStatus(status);
        mapper.updateById(orderBean);

    }

    @Override
    public List<OrderBean> findBySend() {
        return mapper.findBySend();
    }
}
